home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
198_02
/
spawn.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-21
|
20KB
|
748 lines
/* Spawn: various DOS access commands
for MicroEMACS
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
#if AMIGA
#define NEW 1006L
#endif
#if ST520 & MEGAMAX
#include <osbind.h>
#include <string.h>
#define LOAD_EXEC 0 /* load and execute the program */
char *STcmd, /* the command filename & path */
*STargs, /* command args (if any) */
*STenv, /* environment */
*STwork; /* work area */
#endif
#if VMS
#define EFN 0 /* Event flag. */
#include <ssdef.h> /* Random headers. */
#include <stsdef.h>
#include <descrip.h>
#include <iodef.h>
extern int oldmode[3]; /* In "termio.c" */
extern int newmode[3]; /* In "termio.c" */
extern short iochan; /* In "termio.c" */
#endif
#if V7 | USG | BSD
#include <signal.h>
#endif
extern int vttidy();
#if MSDOS & (MSC | TURBO)
#include <process.h>
#endif
/*
* Create a subjob with a copy of the command intrepreter in it. When the
* command interpreter exits, mark the screen as garbage so that you do a full
* repaint. Bound to "^X C". The message at the start in VMS puts out a newline.
* Under some (unknown) condition, you don't get one free when DCL starts up.
*/
spawncli(f, n)
{
#if AMIGA
long newcli;
#endif
#if V7 | USG | BSD
register char *cp;
char *getenv();
#endif
/* don't allow this command if restricted */
if (restflag)
return(resterr());
#if AMIGA
mlwrite("[Starting new CLI]");
sgarbf = TRUE;
Execute("NEWCLI \"CON:0/0/640/200/MicroEMACS Subprocess\"", 0L, 0L);
return(TRUE);
#endif
#if VMS
ttscroll(0, term.t_nrow, 0); /* undo scrolling region */
movecursor(term.t_nrow, 0); /* In last line. */
mlputs("[Starting DCL]\r\n");
TTflush(); /* Ignore "ttcol". */
sgarbf = TRUE;
return (sys(NULL, NULL, NULL)); /* NULL => DCL. */
#endif
#if CPM
mlwrite("Not in CP/M-86");
#endif
#if ST520
mlwrite("Not in TOS");
#endif
#if MSDOS & (AZTEC | MSC | TURBO | C86)
movecursor(term.t_nrow, 0); /* Seek to last line. */
TTflush();
TTkclose();
system("command.com");
TTkopen();
sgarbf = TRUE;
return(TRUE);
#endif
#if MSDOS & LATTICE
movecursor(term.t_nrow, 0); /* Seek to last line. */
TTflush();
TTkclose();
sys("\\command.com", ""); /* Run CLI. */
TTkopen();
sgarbf = TRUE;
return(TRUE);
#endif
#if V7 | USG | BSD
#if TERMCAP
ttscroll(0, term.t_nrow, 0); /* undo scrolling region */
#endif
movecursor(term.t_nrow, 0); /* Seek to last line. */
TTflush();
TTclose(); /* stty to old settings */
if ((cp = getenv("SHELL")) != NULL && *cp != '\0')
system(cp);
else
#if BSD
system("exec /bin/csh");
#else
system("exec /bin/sh");
#endif
sgarbf = TRUE;
sleep(2);
TTopen();
return(TRUE);
#endif
}
#if BSD
bktoshell() /* suspend MicroEMACS and wait to wake up */
{
int pid;
vttidy();
pid = getpid();
kill(pid,SIGTSTP);
return(TRUE);
}
rtfrmshell()
{
TTopen();
curwp->w_flag = WFHARD;
sgarbf = TRUE;
return(TRUE);
}
#endif
#if VMS
bktoshell() /* suspend MicroEMACS and wait to wake up */
{
int err, code, mypid, ownerpid;
/* don't allow this command if restricted */
if (restflag)
return(resterr());
vttidy();
err = 0;
mypid = 0;
code = 771; /* jpi$_owner */
if (lib$getjpi(&code, &mypid, 0, &ownerpid, 0, 0) != 1) err = 1;
else if (ownerpid == 0 || mypid == ownerpid) err = 2;
else if (lib$attach(&ownerpid) != 1) err = 3;
TTopen();
if (err == 0) {
sgarbf = TRUE;
curwp->w_flag = WFHARD;
return(TRUE);
}
if (err == 1) mlwrite("[Error getting pid]");
else if (err == 2) mlwrite("[No parent to attach]");
else mlwrite("[Attach failed]");
return(FALSE);
}
#endif
/*
* Run a one-liner in a subjob. When the command returns, wait for a single
* character to be typed, then mark the screen as garbage so a full repaint is
* done. Bound to "C-X !".
*/
spawn(f, n)
{
register int s;
char line[NLINE];
#if ST520 & MEGAMAX
int i,j,k;
char *sptr,*tptr;
#endif
#if AMIGA
long newcli;
#endif
/* don't allow this command if restricted */
if (restflag)
return(resterr());
#if AMIGA
if ((s=mlreply("!", line, NLINE)) != TRUE)
return (s);
newcli = Open("CON:0/0/640/200/MicroEMACS Subprocess", NEW);
Execute(line, 0L, newcli);
Close(newcli);
tgetc(); /* Pause. */
sgarbf = TRUE;
return(TRUE);
#endif
#if ST520 & MEGAMAX
if ((s=mlreply("!", line, NLINE)) != TRUE)
return(s);
movecursor(term.t_nrow - 1, 0);
TTclose();
/*
* break the line into the command and its args
* be cute about it, if there is no '.' in the filename, try
* to find .prg, .tos or .ttp in that order
* in any case check to see that the file exists before we run
* amok
*/
STenv = NULL;
if((tptr = index(&line[0],' ')) == NULL) { /* no args */
STcmd = (char *)malloc(strlen(line) + 1);
strcpy(STcmd,line);
STargs = NULL;
}
else { /* seperate out the args from the command */
/* resist the temptation to do ptr arithmetic */
STcmd = (char *)malloc(strlen(line) + 1);
for(i = 0,sptr = &line[0]; sptr != tptr; sptr++,i++)
STcmd[i] = *sptr;
STcmd[i] = '\0';
for(; *tptr == ' ' || *tptr == '\t'; tptr++);
if(*tptr == '\0')
STargs = NULL;
else {
STargs = (char *)malloc(strlen(tptr) + 2);
/* first byte of STargs is the length of the string */
STargs[0] = strlen(tptr);
STargs[1] = NULL; /* fake it for strcat */
strcat(STargs,tptr);
}
}
/*
* before we issue the command look for the '.', if it's not there
* try adding .prg, .tos and .ttp to see if they exist, if not
* issue the command as is
*/
if((tptr = index(STcmd,'.')) == NULL) {
STwork = (char *)malloc(strlen(STcmd) + 4);
strcpy(STwork,STcmd);
strcat(STwork,".prg");
tptr = index(STwork,'.');
if(Fsfirst(1,STwork) != 0) { /* try .tos */
strcpy(tptr,".tos");
if(Fsfirst(1,STwork) != 0) { /* try .ttp */
strcpy(tptr,".ttp");
if(Fsfirst(1,STwork) != 0) /* never mind */
*STwork = NULL;
}
}
}
if(*STwork != NULL)
Pexec(LOAD_EXEC,STwork,STargs,STenv);
else
Pexec(LOAD_EXEC,STcmd,STargs,STenv);
TTopen();
mlputs("\r\n\n[End]"); /* Pause. */
TTgetc(); /* Pause. */
sgarbf = TRUE;
return (TRUE);
#endif
#if VMS
ttscroll(0, term.t_nrow, 0); /* undo scrolling region */
if ((s=mlreply("!", line, NLINE)) != TRUE)
return (s);
TTputc('\n'); /* Already have '\r' */
TTflush();
s = sys(line, NULL, NULL); /* Run the command. */
/* if we are interactive, pause here */
if (clexec == FALSE) {
mlputs("\r\n\n[End]"); /* Pause. */
TTflush();
tgetc();
}
sgarbf = TRUE;
return (s);
#endif
#if CPM
mlwrite("Not in CP/M-86");
return (FALSE);
#endif
#if MSDOS | (ST520 & LATTICE)
if ((s=mlreply("!", line, NLINE)) != TRUE)
return(s);
movecursor(term.t_nrow - 1, 0);
TTkclose();
system(line);
TTkopen();
/* if we are interactive, pause here */
if (clexec == FALSE) {
mlputs("\r\n\n[End]");
tgetc();
}
sgarbf = TRUE;
return (TRUE);